package com.dhh.sky.activity;

import java.util.List;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.BaseAdapter;
import android.widget.GridView;
import android.widget.ImageView;
import com.actionbarsherlock.app.SherlockFragmentActivity;
import com.dhh.sky.R;
import com.dhh.sky.model.SkyDriveObject;
import com.dhh.sky.model.SkyDrivePhoto;
import com.dhh.sky.util.Constant;
import com.dhh.sky.util.FileManager;
import com.nostra13.universalimageloader.core.DisplayImageOptions;
import com.nostra13.universalimageloader.core.ImageLoader;
import com.nostra13.universalimageloader.core.ImageLoaderConfiguration;

public class ImageGridActivity extends SherlockFragmentActivity {
	private FileManager fm;
    private DisplayImageOptions options;
    private ImageLoader imageLoader = ImageLoader.getInstance();
	private String path;
	private GridView gridView;
	private ImageAdapter adapter;
	
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
        setContentView(R.layout.image_grid);
        
        path = getIntent().getExtras().getString(Constant.PARAM_PATH);
        fm = new FileManager(this);
        imageLoader.init(ImageLoaderConfiguration.createDefault(this));            
        options = new DisplayImageOptions.Builder()
            .showStubImage(R.drawable.image_picker_placeholder)
            .cacheInMemory()
            .cacheOnDisc()
            .build();
        
        gridView = (GridView) findViewById(R.id.gridview);
        gridView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
			@Override
			public void onItemClick(AdapterView<?> parent, View view,int position, long id) {
				SkyDriveObject item = adapter.getItem(position);
				Intent intent = new Intent(ImageGridActivity.this,ImagePagerActivity.class);
				intent.putExtra(Constant.PARAM_ID, item.getId());
				intent.putExtra(Constant.PARAM_PATH, item.getParentId());
				startActivity(intent);				
			}
		});
        getSupportActionBar().hide();
	}
	
	@Override
	protected void onStart(){
		super.onStart();
		initData();
	}
	
	@Override
	protected void onStop() {
		imageLoader.stop();
		super.onStop();
	}
	
	private void initData(){
		adapter = new ImageAdapter();
		adapter.loadImages();
		gridView.setAdapter(adapter);
	}
	
	
	class ImageAdapter extends BaseAdapter {
		private List<SkyDrivePhoto> photos;
		
		public void loadImages(){
			this.photos = fm.getPhotos(path);
		}
		
		@Override
		public int getCount() {
			return photos.size();
		}

		@Override
		public SkyDrivePhoto getItem(int position) {
			return photos.get(position);
		}

		@Override
		public long getItemId(int position) {
			return position;
		}

		@Override
		public View getView(int position, View convertView, ViewGroup parent) {
			ImageView imageView = (ImageView) convertView;
			if (imageView == null) {
				imageView = (ImageView) getLayoutInflater().inflate(R.layout.item_grid_image, parent, false);
			}

			imageLoader.displayImage(this.getItem(position).getImages()[1].getSource(),imageView, options);

			return imageView;
		}
	}
}
